package com.fastloan.core.handler;

import com.kawins.result.ResultTo;
import com.kawins.shiro.temp.achieve.hander.ShiroExceptionHandler;
import com.kawins.shiro.temp.core.expetion.LockedTimeAccountException;
import com.kawins.shiro.temp.core.expetion.LoginCodeErrorException;
import com.kawins.shiro.temp.core.expetion.LoginCodeOutException;
import com.kawins.shiro.temp.core.result.ShiroResultEnum;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author rankai
 *         createTime 2017-10-2017/10/20 10:15
 */
@RestControllerAdvice
public class GlobalExceptionHandler implements ShiroExceptionHandler {

    @Override
    @ExceptionHandler
    @ResponseStatus(HttpStatus.OK)
    public Object shiroException(UnauthorizedException ex) {
        return new ResultTo(ShiroResultEnum.NOT_AUTHORIZED.getCode(), ShiroResultEnum.NOT_AUTHORIZED.getMessage());
    }

    @Override
    @ExceptionHandler
    @ResponseStatus(HttpStatus.OK)
    public Object shiroException(UnknownAccountException ex) {
        return new ResultTo(ShiroResultEnum.UNKNOWN_ACCOUNT.getCode(), ShiroResultEnum.UNKNOWN_ACCOUNT.getMessage());
    }

    @Override
    @ExceptionHandler(IncorrectCredentialsException.class)
    @ResponseStatus(HttpStatus.OK)
    public Object shiroException(IncorrectCredentialsException ex) {
        return new ResultTo(ShiroResultEnum.INCORRECT_CREDENTIALS.getCode(), ShiroResultEnum.INCORRECT_CREDENTIALS.getMessage());
    }

    @Override
    @ExceptionHandler
    @ResponseStatus(HttpStatus.OK)
    public Object shiroException(LockedAccountException ex) {
        return new ResultTo(ShiroResultEnum.LOCKED_ACCOUNT.getCode(), ShiroResultEnum.LOCKED_ACCOUNT.getMessage());
    }

    @Override
    @ExceptionHandler
    @ResponseStatus(HttpStatus.OK)
    public Object shiroException(DisabledAccountException ex) {
        return new ResultTo(ShiroResultEnum.DISABLED_ACCOUNT.getCode(), ShiroResultEnum.DISABLED_ACCOUNT.getMessage());
    }

    @Override
    @ExceptionHandler
    @ResponseStatus(HttpStatus.OK)
    public Object shiroException(LoginCodeErrorException ex) {
        return new ResultTo(ShiroResultEnum.LOGIN_CODE_ERROR.getCode(), ShiroResultEnum.LOGIN_CODE_ERROR.getMessage());
    }

    @Override
    @ExceptionHandler
    @ResponseStatus(HttpStatus.OK)
    public Object shiroException(LoginCodeOutException ex) {
        return new ResultTo(ShiroResultEnum.LOGIN_CODE_TIMEOUT.getCode(), ShiroResultEnum.LOGIN_CODE_TIMEOUT.getMessage());
    }

    @Override
    public Object shiroException(LockedTimeAccountException e) {
        return null;
    }
}
